<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="本文主要介绍Linux线程的概念以及线程的模型，并介绍基于POSIX线程的接口。">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux线程的概念和使用">
<meta property="og:url" content="http://luqilinok.github.io/2019/07/12/linuxpthread/index.html">
<meta property="og:site_name" content="Lu QiLin&#39;s Blog">
<meta property="og:description" content="本文主要介绍Linux线程的概念以及线程的模型，并介绍基于POSIX线程的接口。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://luqilinok.github.io/2019/07/12/linuxpthread/phread.png">
<meta property="og:image" content="http://luqilinok.github.io/2019/07/12/linuxpthread/thread-1.png">
<meta property="og:image" content="http://luqilinok.github.io/2019/07/12/linuxpthread/thread-3.png">
<meta property="og:updated_time" content="2019-07-12T07:28:08.397Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Linux线程的概念和使用">
<meta name="twitter:description" content="本文主要介绍Linux线程的概念以及线程的模型，并介绍基于POSIX线程的接口。">
<meta name="twitter:image" content="http://luqilinok.github.io/2019/07/12/linuxpthread/phread.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://luqilinok.github.io/2019/07/12/linuxpthread/">





  <title>Linux线程的概念和使用 | Lu QiLin's Blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Lu QiLin's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-technology">
          <a href="/categories/Technology/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Technology
          </a>
        </li>
      
        
        <li class="menu-item menu-item-notes">
          <a href="/categories/Notes/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            Notes
          </a>
        </li>
      
        
        <li class="menu-item menu-item-personal">
          <a href="/categories/Personal/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            Personal
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            About
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://luqilinok.github.io/2019/07/12/linuxpthread/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LuQiLin">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lu QiLin's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Linux线程的概念和使用</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-07-12T15:13:56+08:00">
                2019-07-12
              </time>
            

            

            
          </span>
          
  <span class="post-updated">
    &nbsp; | &nbsp; 更新于
    <time itemprop="dateUpdated" datetime="2019-07-12T15:28:08+08:00" content="2019-07-12">
      2019-07-12
    </time>
  </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Notes/" itemprop="url" rel="index">
                    <span itemprop="name">Notes</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>本文主要介绍Linux线程的概念以及线程的模型，并介绍基于POSIX线程的接口。</p>
<a id="more"></a>
<hr>
<h1 id="什么是线程？"><a href="#什么是线程？" class="headerlink" title="什么是线程？"></a>什么是线程？</h1><ul>
<li>在一个程序里的一个执行路线就叫做线程（thread）。更准确的定义是：线程是“一个进程内部的控制序列”。</li>
<li>一切进程至少都有一个执行线程。</li>
</ul>
<hr>
<h1 id="进程与线程"><a href="#进程与线程" class="headerlink" title="进程与线程"></a>进程与线程</h1><ul>
<li>进程是资源分配与竞争的基本单位</li>
<li>线程是程序执行的最小单位</li>
<li>线程共享进程数据，但也有自己的一部分数据<ul>
<li>线程ID</li>
<li>一组寄存器：IP，PSW，堆栈指针</li>
<li>栈</li>
<li>errno</li>
<li>信号状态</li>
<li>优先级<br><img src="/2019/07/12/linuxpthread/phread.png" alt></li>
</ul>
</li>
</ul>
<hr>
<h1 id="fork和创建新线程的区别"><a href="#fork和创建新线程的区别" class="headerlink" title="fork和创建新线程的区别"></a>fork和创建新线程的区别</h1><ul>
<li>当一个进程执行一个fork调用的时候，会创建出进程的一个新拷贝，新进程将拥有它自己的变量和它自己的PID。这个新进程的运行时间是独立的，它在执行时几乎完全独立于创建它的进程。</li>
<li>在进程里面创建一个新线程的时候，新的执行进程会拥有自己的堆栈（因此也就有自己的局部变量），但要与他的创建者共享全局变量、文件描述符、信号处理器和当前工作目录状态。</li>
</ul>
<hr>
<h1 id="线程的优点"><a href="#线程的优点" class="headerlink" title="线程的优点"></a>线程的优点</h1><ul>
<li>创建一个新线程的代价要比创建一个新进程小得多</li>
<li>与进程之间的切换相比，线程之间的切换需要操作系统做的工作少得多</li>
<li>线程占用的资源要比进程少得多</li>
<li>能充分利用多处理器的可并行数量</li>
<li>在等待慢速I/O操作结束的同时，程序可执行其他的计算任务</li>
<li>计算密集型应用，为了能在多处理器系统上运行，将计算分解到多个线程中实现</li>
<li>I/O密集型应用，为了提高系统，将I/O操作重叠。线程可以同时等待不同的I/O操作</li>
</ul>
<hr>
<h1 id="线程的缺点"><a href="#线程的缺点" class="headerlink" title="线程的缺点"></a>线程的缺点</h1><ul>
<li>性能损失<ul>
<li>一个很少被外部事件阻塞的计算密集型线程往往无法与其他线程共享同一个处理器。如果计算密集型线程的数量比可用的处理器多，那么可能会有较大的性能损失，这里的性能损失指的是增加了额外的同步和调度开销，而可用的资源不变。</li>
</ul>
</li>
<li>健壮性降低<ul>
<li>编写多线程需要更全面更深入的考虑，在一个多线程程序里，因时间分配上的细微偏差或者因共享了不该共享的变量而造成不良的可能性是很大的，换句话说线程之间是缺乏保护的。</li>
</ul>
</li>
<li>缺乏访问控制<ul>
<li>进程是访问控制的基本粒度，在一个线程中调用某些OS函数会对整个进程造成影响。</li>
</ul>
</li>
<li>编程难度提高<ul>
<li>编写与调试一个多线程比单线程程序困难得多</li>
</ul>
</li>
</ul>
<hr>
<h1 id="线程调度竞争范围"><a href="#线程调度竞争范围" class="headerlink" title="线程调度竞争范围"></a>线程调度竞争范围</h1><ul>
<li>操作系统提供了各个模型，用来调度应用程序创建的线程。这些模型时间的主要不同是：在竞争系统资源（特别是CPU时间）时，线程调度竞争范围（thread-scheduling contention scope）不一样</li>
<li>进程竞争范围（Process contention scope）：各个线程在同一个进程竞争“被调度的CPU时间”（但不直接和其他进程中的线程竞争）。</li>
<li>系统竞争范围（System contention scope）：线程直接和”系统范围“内的其他线程竞争。</li>
</ul>
<hr>
<h1 id="线程模型"><a href="#线程模型" class="headerlink" title="线程模型"></a>线程模型</h1><h2 id="N-1用户线程模型"><a href="#N-1用户线程模型" class="headerlink" title="N:1用户线程模型"></a>N:1用户线程模型</h2><ul>
<li>“线程实现”建立在“进程控制”机制之上，由用户空间的程序库来管理。OS内核完全不知道线程信息。这些线程成为用户空间线程。</li>
<li>这些线程都工作在“进程竞争范围”<br><img src="/2019/07/12/linuxpthread/thread-1.png" alt></li>
<li>在N:1线程模型中，内核不干涉线程的任何生命活动，也不干涉同一进程中的线程环境切换</li>
<li>在N:1线程模型中，一个进程中的多个线程只能调度到一个CPU，这种约束限制了可用的并行总量。</li>
<li>第二个缺点是如果某个线程执行了一个“阻塞式”操作（如read），那么，进程中的所有线程都会阻塞，直至那个操作结束。为此，一个线程的实现是为这些阻塞式函数提供包装器，用非阻塞版本替换这些系统调用，以清除这种限制。</li>
</ul>
<hr>
<h2 id="1-1核心线程模型"><a href="#1-1核心线程模型" class="headerlink" title="1:1核心线程模型"></a>1:1核心线程模型</h2><ul>
<li>在1:1核心线程模型中，应用程序创建的每一个线程都由一个核心线程直接管理。</li>
<li>OS内核将每一个核心线程都调度到系统CPU上，因此，所有线程都工作在“系统竞争范围”。</li>
<li>这种线程的创建于调度由内核完成，因为这种线程的系统开销比较大（但一般来说，比进程开销小）</li>
</ul>
<hr>
<h2 id="N-M混合线程模型"><a href="#N-M混合线程模型" class="headerlink" title="N:M混合线程模型"></a>N:M混合线程模型</h2><ul>
<li>N:M混合线程模式提供了两级控制，将用户线程映射为系统的可调度体以实现并行，这个可调度体称为轻量级进程（LWP：lightweight process），LWP再一一映射到核心线程。<br><img src="/2019/07/12/linuxpthread/thread-3.png" alt></li>
</ul>
<hr>
<h1 id="POSIX线程库"><a href="#POSIX线程库" class="headerlink" title="POSIX线程库"></a>POSIX线程库</h1><h2 id="pthread-create函数"><a href="#pthread-create函数" class="headerlink" title="pthread_create函数"></a>pthread_create函数</h2><ul>
<li><p>功能：创建一个新的线程</p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_create</span><span class="params">(<span class="keyword">pthread_t</span> *thread, <span class="keyword">const</span> <span class="keyword">pthread_attr_t</span> *attr, <span class="keyword">void</span> *(*start_routine)(<span class="keyword">void</span> *), <span class="keyword">void</span> *arg)</span></span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>thread：返回线程ID</li>
<li>attr：设置线程的属性，attr为NULL表示使用默认属性</li>
<li>start_routine：是个函数地址，线程启动后要执行的函数</li>
<li>arg：传给线程启动函数的参数</li>
</ul>
</li>
<li><p>返回值：成功返回0，失败返回错误码</p>
</li>
</ul>
<hr>
<h2 id="pthread-join函数"><a href="#pthread-join函数" class="headerlink" title="pthread_join函数"></a>pthread_join函数</h2><ul>
<li><p>功能：等待线程结束</p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_join</span><span class="params">(<span class="keyword">pthread_t</span> thread, <span class="keyword">void</span> **retval)</span></span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>thread：线程ID</li>
<li>retval：它指向一个指针，后者指向线程的返回值</li>
</ul>
</li>
<li><p>参数：成功返回0，失败返回错误码</p>
</li>
</ul>
<hr>
<h2 id="pthread-exit函数"><a href="#pthread-exit函数" class="headerlink" title="pthread_exit函数"></a>pthread_exit函数</h2><ul>
<li><p>功能：线程终止</p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">pthread_exit</span><span class="params">(<span class="keyword">void</span> *value_ptr)</span></span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>value_ptr：value_ptr不要指向一个局部变量。</li>
</ul>
</li>
<li><p>返回值：无返回值，跟进程一样，线程结束的时候无法返回到它的调用者（自身）</p>
</li>
</ul>
<hr>
<h2 id="pthread-self函数"><a href="#pthread-self函数" class="headerlink" title="pthread_self函数"></a>pthread_self函数</h2><ul>
<li><p>功能：返回线程ID</p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">pthread_t</span> pthread_self(<span class="keyword">void</span>);</span><br></pre></td></tr></table></figure>
</li>
<li><p>返回值：成功返回0</p>
</li>
</ul>
<hr>
<h2 id="pthread-cancel"><a href="#pthread-cancel" class="headerlink" title="pthread_cancel"></a>pthread_cancel</h2><ul>
<li><p>功能：取消一个执行的线程</p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_cancel</span><span class="params">(<span class="keyword">pthread_t</span> thread)</span></span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>thread：线程ID</li>
</ul>
</li>
<li><p>返回值：成功返回0；失败返回错误码</p>
</li>
</ul>
<hr>
<h2 id="pthread-detach函数"><a href="#pthread-detach函数" class="headerlink" title="pthread_detach函数"></a>pthread_detach函数</h2><ul>
<li><p>功能：将一个线程分离</p>
</li>
<li><p>原型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_detach</span><span class="params">(<span class="keyword">pthread_t</span> thread)</span></span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数：</p>
<ul>
<li>thread：线程ID</li>
</ul>
</li>
<li><p>返回值：成功返回0；失败返回错误码</p>
</li>
</ul>
<hr>
<h2 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;pthread.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ERR_EXIT(m)         \</span></span><br><span class="line">    <span class="keyword">do</span>                      \</span><br><span class="line">    &#123;                       \</span><br><span class="line">        perror(m);          \</span><br><span class="line">        <span class="built_in">exit</span>(EXIT_FAILURE); \</span><br><span class="line">    &#125; <span class="keyword">while</span> (<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> *<span class="title">thread_routine</span><span class="params">(<span class="keyword">void</span> *arg)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i;</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">20</span>; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"B"</span>);</span><br><span class="line">        fflush(<span class="built_in">stdout</span>);</span><br><span class="line">        usleep(<span class="number">20</span>);</span><br><span class="line">        <span class="keyword">if</span> (i == <span class="number">3</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            pthread_exit(<span class="string">"ABC"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">pthread_t</span> tid;</span><br><span class="line">    <span class="keyword">int</span> ret;</span><br><span class="line">    <span class="keyword">if</span> ((ret = pthread_create(&amp;tid, <span class="literal">NULL</span>, thread_routine, <span class="literal">NULL</span>)) != <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">"pthread_create: %s\n"</span>, strerror(ret));</span><br><span class="line">        <span class="built_in">exit</span>(EXIT_FAILURE);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> i;</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">20</span>; ++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"A"</span>);</span><br><span class="line">        fflush(<span class="built_in">stdout</span>);</span><br><span class="line">        usleep(<span class="number">20</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">void</span> *value;</span><br><span class="line">    <span class="keyword">if</span> ((ret = pthread_join(tid, &amp;value)) != <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">"pthread_join: %s\n"</span>, strerror(ret));</span><br><span class="line">        <span class="built_in">exit</span>(EXIT_FAILURE);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">" msg = %s\n"</span>, (<span class="keyword">char</span> *)value);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="线程属性"><a href="#线程属性" class="headerlink" title="线程属性"></a>线程属性</h1><h2 id="初始化与销毁属性"><a href="#初始化与销毁属性" class="headerlink" title="初始化与销毁属性"></a>初始化与销毁属性</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_init</span><span class="params">(<span class="keyword">pthread_attr_t</span> *attr)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_destroy</span><span class="params">(<span class="keyword">pthread_attr_t</span> *attr)</span></span>;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="获取与设置分离属性"><a href="#获取与设置分离属性" class="headerlink" title="获取与设置分离属性"></a>获取与设置分离属性</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_getdetachstate</span><span class="params">(<span class="keyword">const</span> <span class="keyword">pthread_attr_t</span> \*attr, <span class="keyword">int</span> \*detachstate)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_setdetachstate</span><span class="params">(<span class="keyword">const</span> <span class="keyword">pthread_attr_t</span> *attr, <span class="keyword">int</span> detachstate)</span></span>;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="获取与设置栈大小"><a href="#获取与设置栈大小" class="headerlink" title="获取与设置栈大小"></a>获取与设置栈大小</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_setstacksize</span><span class="params">(<span class="keyword">pthread_attr_t</span> *attr, <span class="keyword">size_t</span> stacksize)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_getstacksize</span><span class="params">(<span class="keyword">pthread_attr_t</span> \*attr, <span class="keyword">size_t</span> \*stacksize)</span></span>;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="获取与设置栈溢出保护区大小"><a href="#获取与设置栈溢出保护区大小" class="headerlink" title="获取与设置栈溢出保护区大小"></a>获取与设置栈溢出保护区大小</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_setguardsize</span><span class="params">(<span class="keyword">pthread_attr_t</span> *attr, <span class="keyword">size_t</span> guardsize)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_getguardsize</span><span class="params">(<span class="keyword">pthread_attr_t</span> *attr, <span class="keyword">size_t</span> *guardsize)</span></span>;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="获取与设置线程竞争范围"><a href="#获取与设置线程竞争范围" class="headerlink" title="获取与设置线程竞争范围"></a>获取与设置线程竞争范围</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_setscope</span><span class="params">(<span class="keyword">pthread_attr_t</span> *attr, <span class="keyword">int</span> scope)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_getscope</span><span class="params">(<span class="keyword">pthread_attr_t</span> *attr, <span class="keyword">int</span> *scope)</span></span>;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="获取与设置调度策略"><a href="#获取与设置调度策略" class="headerlink" title="获取与设置调度策略"></a>获取与设置调度策略</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_setschedpolicy</span><span class="params">(<span class="keyword">pthread_attr_t</span> *attr, <span class="keyword">int</span> policy)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_getschedpolicy</span><span class="params">(<span class="keyword">pthread_attr_t</span> *attr, <span class="keyword">int</span> *policy)</span></span>;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="获取与设置继承的调度策略"><a href="#获取与设置继承的调度策略" class="headerlink" title="获取与设置继承的调度策略"></a>获取与设置继承的调度策略</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_setinheritsched</span><span class="params">(<span class="keyword">pthread_attr_t</span> *attr, <span class="keyword">int</span> inheritsched)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_getinheritsched</span><span class="params">(<span class="keyword">pthread_attr_t</span> *attr, <span class="keyword">int</span> *inheritsched)</span></span>;</span><br></pre></td></tr></table></figure>
<h2 id="获取与设置调度参数"><a href="#获取与设置调度参数" class="headerlink" title="获取与设置调度参数"></a>获取与设置调度参数</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_setschedparam</span><span class="params">(<span class="keyword">pthread_attr_t</span> *attr, <span class="keyword">const</span> struct sched_param *param)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_attr_getschedparam</span><span class="params">(<span class="keyword">pthread_attr_t</span> *attr, struct sched_param *param)</span></span>;</span><br></pre></td></tr></table></figure>
<hr>
<h2 id="获取与设置并发级别"><a href="#获取与设置并发级别" class="headerlink" title="获取与设置并发级别"></a>获取与设置并发级别</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_setconcurrency</span><span class="params">(<span class="keyword">int</span> new_level)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_getconcurrency</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br></pre></td></tr></table></figure>
<ul>
<li>仅在N:1线程模型中有效，设置并发级别，给内核一个提示：表示提供给定级别数量的核心线程来映射用户线程是有效的。</li>
</ul>
<hr>
<h1 id="线程特定数据"><a href="#线程特定数据" class="headerlink" title="线程特定数据"></a>线程特定数据</h1><ul>
<li><p>在单线程程序中，我们经常要用到全局变量以实现多个函数间共享数据</p>
</li>
<li><p>在多线程环境下，由于数据空间是共享的，因此全局变量也为所有线程所共有</p>
</li>
<li><p>但有时应用程序设计中有必要提供线程的私有的全局变量，仅在某个线程中有效，但却可以跨多个函数访问</p>
</li>
<li><p>POSIX线程库通过维护一定的数据结构来解决这个问题，这些数据成为（Thread-sepcific Data，或TSD）</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_key_create</span><span class="params">(<span class="keyword">pthread_key_t</span> *key, <span class="keyword">void</span> (*destructor)(<span class="keyword">void</span>*))</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_key_delete</span><span class="params">(<span class="keyword">pthread_key_t</span> key)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> *<span class="title">pthread_getspecific</span><span class="params">(<span class="keyword">pthread_key_t</span> key)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_setspecific</span><span class="params">(<span class="keyword">pthread_key_t</span> key, <span class="keyword">const</span> <span class="keyword">void</span> *value)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">pthread_once</span><span class="params">(<span class="keyword">pthread_once_t</span> *once_control, <span class="keyword">void</span> (*init_routine)(<span class="keyword">void</span>))</span></span>;</span><br><span class="line"><span class="keyword">pthread_once_t</span> once_control = PTHREAD_ONCE_INIT;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>实例：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;pthread.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ERR_EXIT(m)         \</span></span><br><span class="line">    <span class="keyword">do</span>                      \</span><br><span class="line">    &#123;                       \</span><br><span class="line">        perror(m);          \</span><br><span class="line">        <span class="built_in">exit</span>(EXIT_FAILURE); \</span><br><span class="line">    &#125; <span class="keyword">while</span> (<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">tsd</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">pthread_t</span> tid;</span><br><span class="line">    <span class="keyword">char</span> *str;</span><br><span class="line">&#125; <span class="keyword">tsd_t</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">pthread_key_t</span> key_tsd;</span><br><span class="line"><span class="keyword">pthread_once_t</span> once_control = PTHREAD_ONCE_INIT;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">destroy_routine</span><span class="params">(<span class="keyword">void</span> *value)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"destroy\n"</span>);</span><br><span class="line">    <span class="built_in">free</span>(value);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init_routine</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    pthread_key_create(&amp;key_tsd, destroy_routine);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> *<span class="title">thread_routine</span><span class="params">(<span class="keyword">void</span> *arg)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    pthread_once(&amp;once_control, init_routine);</span><br><span class="line">    <span class="keyword">tsd_t</span> *value = (<span class="keyword">tsd_t</span> *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">tsd_t</span>));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">tsd_t</span> *value = (<span class="keyword">tsd_t</span> *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">tsd_t</span>));</span><br><span class="line">    value-&gt;tid = pthread_self();</span><br><span class="line">    value-&gt;str = (<span class="keyword">char</span> *)arg;</span><br><span class="line">    pthread_setspecific(key_tsd, value);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"%s setspecific %p\n"</span>, (<span class="keyword">char</span> *)arg, value);</span><br><span class="line">    value = pthread_getspecific(key_tsd);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"tid=0x%x str=%s\n"</span>, value-&gt;tid, value-&gt;str);</span><br><span class="line">    sleep(<span class="number">2</span>);</span><br><span class="line">    value = pthread_getspecific(key_tsd);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"tid=0x%x str=%s\n"</span>, value-&gt;tid, value-&gt;str);</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// pthread_key_create(&amp;key_tsd, destroy_routine);</span></span><br><span class="line">    <span class="keyword">pthread_t</span> tid1;</span><br><span class="line">    <span class="keyword">pthread_t</span> tid2;</span><br><span class="line">    <span class="keyword">int</span> ret;</span><br><span class="line">    <span class="keyword">if</span> ((ret = pthread_create(&amp;tid1, <span class="literal">NULL</span>, thread_routine, <span class="string">"thread1"</span>)) != <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">"pthread_create: %s\n"</span>, strerror(ret));</span><br><span class="line">        <span class="built_in">exit</span>(EXIT_FAILURE);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> ((ret = pthread_create(&amp;tid2, <span class="literal">NULL</span>, thread_routine, <span class="string">"thread2"</span>)) != <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">"pthread_create: %s\n"</span>, strerror(ret));</span><br><span class="line">        <span class="built_in">exit</span>(EXIT_FAILURE);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    pthread_join(tid1, <span class="literal">NULL</span>);</span><br><span class="line">    pthread_join(tid1, <span class="literal">NULL</span>);</span><br><span class="line"></span><br><span class="line">    pthread_key_delete(key_tsd);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/07/12/linuxposixmemory/" rel="next" title="POSIX 共享内存">
                <i class="fa fa-chevron-left"></i> POSIX 共享内存
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/07/12/linuxposixsemphoreandmutex/" rel="prev" title="POSIX信号量与互斥锁">
                POSIX信号量与互斥锁 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">LuQiLin</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">80</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#什么是线程？"><span class="nav-number">1.</span> <span class="nav-text">什么是线程？</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#进程与线程"><span class="nav-number">2.</span> <span class="nav-text">进程与线程</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#fork和创建新线程的区别"><span class="nav-number">3.</span> <span class="nav-text">fork和创建新线程的区别</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#线程的优点"><span class="nav-number">4.</span> <span class="nav-text">线程的优点</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#线程的缺点"><span class="nav-number">5.</span> <span class="nav-text">线程的缺点</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#线程调度竞争范围"><span class="nav-number">6.</span> <span class="nav-text">线程调度竞争范围</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#线程模型"><span class="nav-number">7.</span> <span class="nav-text">线程模型</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#N-1用户线程模型"><span class="nav-number">7.1.</span> <span class="nav-text">N:1用户线程模型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1核心线程模型"><span class="nav-number">7.2.</span> <span class="nav-text">1:1核心线程模型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#N-M混合线程模型"><span class="nav-number">7.3.</span> <span class="nav-text">N:M混合线程模型</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#POSIX线程库"><span class="nav-number">8.</span> <span class="nav-text">POSIX线程库</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#pthread-create函数"><span class="nav-number">8.1.</span> <span class="nav-text">pthread_create函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#pthread-join函数"><span class="nav-number">8.2.</span> <span class="nav-text">pthread_join函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#pthread-exit函数"><span class="nav-number">8.3.</span> <span class="nav-text">pthread_exit函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#pthread-self函数"><span class="nav-number">8.4.</span> <span class="nav-text">pthread_self函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#pthread-cancel"><span class="nav-number">8.5.</span> <span class="nav-text">pthread_cancel</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#pthread-detach函数"><span class="nav-number">8.6.</span> <span class="nav-text">pthread_detach函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实例"><span class="nav-number">8.7.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#线程属性"><span class="nav-number">9.</span> <span class="nav-text">线程属性</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#初始化与销毁属性"><span class="nav-number">9.1.</span> <span class="nav-text">初始化与销毁属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#获取与设置分离属性"><span class="nav-number">9.2.</span> <span class="nav-text">获取与设置分离属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#获取与设置栈大小"><span class="nav-number">9.3.</span> <span class="nav-text">获取与设置栈大小</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#获取与设置栈溢出保护区大小"><span class="nav-number">9.4.</span> <span class="nav-text">获取与设置栈溢出保护区大小</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#获取与设置线程竞争范围"><span class="nav-number">9.5.</span> <span class="nav-text">获取与设置线程竞争范围</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#获取与设置调度策略"><span class="nav-number">9.6.</span> <span class="nav-text">获取与设置调度策略</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#获取与设置继承的调度策略"><span class="nav-number">9.7.</span> <span class="nav-text">获取与设置继承的调度策略</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#获取与设置调度参数"><span class="nav-number">9.8.</span> <span class="nav-text">获取与设置调度参数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#获取与设置并发级别"><span class="nav-number">9.9.</span> <span class="nav-text">获取与设置并发级别</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#线程特定数据"><span class="nav-number">10.</span> <span class="nav-text">线程特定数据</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LuQiLin</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
